#include "../include/FontNode.hpp"
using namespace klas;

FontNode::FontNode()
{
	m_font = NULL;
	m_shouldRender = true;
}

FontNode::FontNode(DataObject* font)
{
	setFont(font);
	m_rot = 0.0;
	m_scale = 1.0;
	m_ID = INVALID_ID;
}

FontNode::FontNode(DataObject* font, string text)
{
	setFont(font);
	setText(text);
	m_rot = 0.0;
	m_scale = 1.0;
	m_ID = INVALID_ID;
}

FontNode::~FontNode()
{
}

void FontNode::setFont(DataObject* font)
{
  if(font->getObjectType() == Data::FONT)
    m_font = (Font*)font;

}

void FontNode::setText(string text)
{
	m_text = text;
}

string FontNode::getText() const
{
	return m_text;
}

FontNode* FontNode::getDeepCopy()
{
	FontNode* node = new FontNode();
	node->m_font = m_font;
	node->m_text = m_text;
	node->m_baseColor = m_baseColor;
	node->m_rot = m_rot;
	node->m_scale = m_scale;
	node->m_ID = m_ID;
	node->m_rect.setPos(m_rect.getPos());
	node->m_rect.setDim(m_rect.getDim());
	node->m_prevRect.setPos(m_prevRect.getPos());
	node->m_prevRect.setDim(m_prevRect.getDim());
	
	return node;
}

iReal FontNode::getNodeType() const
{
	return Node::FONT;
}

void __rot2D(float angle, float pointOfRotationX, float pointOfRotationY)
{
  // Note that the steps here are in the reverse order of that mentioned above!!
  // That's because openGL transformations post-multiply the matrices.
  glTranslatef(pointOfRotationX, pointOfRotationY, 0.0f);
  glRotatef(angle, 0.0f, 0.0f, 1.0f);
  glTranslatef(-pointOfRotationX, -pointOfRotationY, 0.0f);
}

void FontNode::render(const Rectangle<Real>& camera)
{
  if(m_font == NULL || m_shouldRender == false)
    return;

  glPushMatrix();


  __rot2D(m_rot, (m_rect.x - camera.x), (m_rect.y - camera.y));
  glTranslated(m_rect.x - camera.x, m_rect.y - camera.y, 0.0);
  
  glScaled(m_scale, m_scale, 0.0);
  glColor4f(m_baseColor.getR(), m_baseColor.getG(), m_baseColor.getB(), m_baseColor.getA());
  
  m_font->drawText(0, 0, m_text);


  glPopMatrix();
}
